package com.wonder.app.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.StkTransferApp;
import com.wonder.app.domain.StkTransferAppDetaill;
import com.wonder.app.service.StkApplyService;
import com.wonder.common.common.AjaxResult;
import com.wonder.framework.authorization.CheckAdminToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "出库申请单")
@RestController
@RequestMapping("/app/Stkapply")
public class StkApplyController {
    @Autowired
    private StkApplyService stkApplyService;

    @Autowired
    private SysAdminService adminService;

    @Autowired
    private CheckAdminToken checkAdminToken;

    /**
     * @explain 金蝶审核其他出库申请单
     * @param
     * @return  Boolean
     * @author  Wonder
     */
    @PostMapping("/audit")
    @ApiOperation(value = "审核", notes = "作者：Wonder")
    public AjaxResult<Boolean> auditStkApply(@RequestBody JSONObject map) throws Exception{
        if (null != map) {
            boolean res = stkApplyService.audit(map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }

    @ApiOperation(value = "出库申请单列表(手机端)")
    @RequestMapping(value = "/get_stk_apply_list", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getStkApplyList(@RequestBody Map<String,Object> map) {
        Map<String,Object> data = stkApplyService.getStkApplyList(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }


    @ApiOperation(value = "出库申请单详情(手机端)")
    @RequestMapping(value = "/get_stk_apply_detail", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> getStkApplyDetail(@RequestBody Map<String,Object> map) {
        Map<String,Object> data = stkApplyService.getStkApplyDetail(map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }

    @ApiOperation(value = "审核其他出库申请(手机端)")
    @RequestMapping(value = "/audit_stk_apply", method = RequestMethod.POST)
    public AjaxResult<?> auditStkApply(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Boolean data = stkApplyService.auditStkApply(userName,user_id,map);
        if (data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }


    @ApiOperation(value = "推荐仓位")
    @RequestMapping(value = "/commend_location", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> commendLocation(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Map<String,Object> data = stkApplyService.commendLocation(user_id,map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }


    @ApiOperation(value = "根据件号和仓库查询库存")
    @RequestMapping(value = "/commend_location_by_item", method = RequestMethod.POST)
    public AjaxResult<Map<String,Object>> commendLocationByItem(HttpServletRequest request, @RequestBody Map<String,Object> map) throws Exception {
        String token = checkAdminToken.getTokenFormRequest(request);
        SysAdmin user = adminService.getAdminByToken(token);
        Long user_id = user.getUser_id();
        String userName = user.getName();
        Map<String,Object> data = stkApplyService.commendLocationByItem(user_id,map);
        if (null != data){
            return AjaxResult.success(data);
        } else {
            return AjaxResult.failed();
        }
    }


    /**
     * 出库申请整单关闭
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/stk_apply_close")
    @ApiOperation(value = "出库申请整单关闭", notes = "作者：Wonder")
    public AjaxResult<Boolean> receiveClose(@RequestBody JSONObject map) throws Exception{
        if (null != map) {
            boolean res = stkApplyService.applyClose(map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }


    /**
     * 出库申请单反审核
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/unaudit")
    @ApiOperation(value = "审核", notes = "作者：Wonder")
    public AjaxResult<Boolean> unaudit(@RequestBody JSONObject map) throws Exception{
        if (null != map) {
            boolean res = stkApplyService.unAudit(map);
            if (res){
                return AjaxResult.success(res);
            } else {
                return AjaxResult.failed();
            }
        }else {
            return AjaxResult.failed("system.parameterError");
        }
    }
}
